package com.pig.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/**
 * 播放一次音乐
 * 
 * @author Domili
 * 
 */
public class OnceMusic {

	private AudioStream as;

	private String filePath;

	/**
	 * 构造方法，初始化同时按音频文件路径加载音频文件
	 * 
	 * @param musicPath
	 *            音频文件相对路径
	 */
	public OnceMusic(String musicPath) {
		filePath = musicPath;
		load(filePath);
	}

	/**
	 * 加载音频文件
	 * 
	 * @param musicPath
	 *            音频文件的相对路径
	 */
	public void load(String musicPath) {
		FileUtil fUtil = new FileUtil();
		String musicRealPath = fUtil.getFilePath(musicPath);
		try {
			as = new AudioStream(new FileInputStream(musicRealPath));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 单次播放音频文件
	 */
	public void play() {
		AudioPlayer.player.start(as);
	}

	public void stop() {
		AudioPlayer.player.stop(as);
	}
}
